package org.crazyit.cloud.error;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @Author JLL
 * @Date 2019/2/12/0012 下午 6:07
 */
public class ErrorCommand extends HystrixCommand<String> {


    public ErrorCommand() {
        super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
    }

    @Override
    protected String run() throws Exception {
        //不同的是，这里是去调用一个超时的地址
        String url = "http://localhost:8080/errorHello";
        HttpGet httpGet = new HttpGet(url);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpResponse response = httpClient.execute(httpGet);
        return EntityUtils.toString(response.getEntity());
    }


    @Override
    protected String getFallback() {

        System.out.println("fall back method");
        //就是返回这个字符串来代替原来的请求。
        return "fall back hello";
    }
}
